<?php 

namespace Weixin\Controller;
use Common\Controller\WeixinBase;
use Common\Model\Model;

class SuipaiController extends WeixinbaseController
{
	
	public function index()
	{
	    //作品列表
        $Model=D('Weixin/Suipai');
        $where['status']=array('EQ',true);

        $count = $Model->where($where)->count();
        $page = $this->Pages($count, 5);
        
        $list=$Model->getSpList($this->userid, $page->firstRow,$page->listRows);
        $this->assign("list", $list);
        $this->getJsdk();
        $this->assign('pageType', 'suipai');
// 		var_dump($list);
        $this->display();
	}

	

	public function splist(){
	
	    //作品列表
	    $Model=D('Weixin/Suipai');
	    $where['status']=array('EQ',true);
	   
	    $count = $Model->where($where)->count();
	    $page = $this->Pages($count, 5);
	   
	    $list['total']=5;
	   
	    $list['result']=$Model->getSpList($this->userid, $page->firstRow,$page->listRows);
	        
	    exit(json_encode($list));
	}
	
	
	public function ssplist(){
	
	    //作品列表
	    $Model=D('Weixin/Suipai');
	    $where['status']=array('EQ',true);
	
	    $count = $Model->where($where)->count();
	    $page = $this->Pages($count, 5);
	
	    $res['code']=0;
	    $res['msg']="操作成功";
	    $res['data']=$Model->getSpList2($this->userid, $page->firstRow,$page->listRows);

	    if(isset($_REQUEST['callback'])){
	        
	         echo  $_REQUEST['callback']."(".json_encode($res).")";
	    }else{
	        
	        echo json_encode($res);
	    }
	    
	    exit;
	    
	}
	
	public function add()
	{
	    if(IS_POST)
	    {
	        $arr = array();
        	$data['title'] = $_POST['title'];
        	$data['content'] = $_POST['content'];

        	if(mb_strlen($data['title'], 'utf-8') < 1 || mb_strlen($data['title'], 'utf-8') > 30){
        	    $this->error('作品名称不能为空且不能超过30个字符');
        	}
        	if(empty($data['content'])){
        	    $this->error('请添加作品说明');
        	}
        	//$_POST['all_pic'] = array('0' => 'LjBAtyTU6BszkfBll_AJRmxaaLoa2tMMwxMJgn741JKhCe38dJMet4W9Sjavg_YB');
        	if(count($_POST['all_pic']) > 0)
        	{
            	$arr = $this->getPic($_POST['all_pic']);
        	}
        	else 
        	{
        	    $this->error('请上传至少一张作品图');
        	}
        	$data['image'] = $arr['image'];
        	$data['all_pic'] = json_encode($arr['thumb']);
        	
            $post = $data;
            $post['userid']=$this->userid;
            $model=D('Weixin/Suipai');
            $status = $model->suipaiAddModel($post);
            if ($status) {
                $this->success("作品添加成功,请等待审核！", U('Suipai/index'));
            } else {
                $error = $model->getError();
                $this->error($error ? $error : '添加失败！');
            }
	    }
	    $this->isLogin();
	    $this->getJsdk();
	    $this->display();
	}
	
	public function detail()
	{
	    $id = I('get.id');
	    if(empty($id)){
	        header('Location:'.WEB_URL.U('suipai/index'));
	        exit;
	    }
	    $model = M('Suipai');
	    $model->where(array('id' => $id))->setInc('clicknum');
	    $unionModel=new Model();
	    
	    $detail = $unionModel->table(array(
	        C('DB_PREFIX').'user'=>'U',
	        C('DB_PREFIX').'suipai'=>'S',
	    ))
	    ->where('U.userid=S.userid and S.id='.$id)
	    ->field('U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz,S.id,S.title,S.image,S.all_pic,S.content,S.clicknum,S.msgnum,S.create_time')
	    ->find();
	    
	    if(!$detail){
	        header('Location:'.WEB_URL.U('suipai/index'));
	        exit;
	    }
        
	    $yf_thumb = array();
	    $yf_pic = array();
	    if(!empty($detail['all_pic']))
	    {
	        $yf_arr = json_decode($detail['all_pic'], true);
	        $yf_temp = array();
	        foreach($yf_arr as $k => $v)
	        {
	            $yf_ex = explode("/", $v);
	            $yf_ex[count($yf_ex)-1] = str_replace("yf_", "", $yf_ex[count($yf_ex)-1]);
	            $yf_pic[] = $temp = WEB_URL.implode("/", $yf_ex);
	            $yf_thumb[] = array(
	                'thumb' => $v,
	                'pic' => $temp
	            );
	        }
	    }
	    else
	    {
	        $yf_arr = array();
	        $yf_thumb[] = array(
	            'thumb' => $detail['image'],
	            'pic' => WEB_URL.$detail['image']
	        );
	        $yf_pic[] = WEB_URL.$detail['image'];
	    }
	    $detail['pic_num'] = count($yf_thumb);
	    $detail['thumb'] = $yf_thumb;
	    $detail['pic'] = implode(",", $yf_pic);
	    //end
	    
	    $this->assign('data', $detail);
	    
	    $where['infoid'] = $id;
	    $where['type'] = 4;
	    $where['parent_id'] = 0;//yf 0915
	    $count = M('UserMessage')->where($where)->count();
	    
	    // $limitRows = 5; // 设置每页记录数
	    $limitRows = C('YF_PAGE');
	    $page = $this->ajaxpage($count, $limitRows, '#msg', 'ajax_div', 'ajax_page_div');
	    
	    $message=$unionModel
	    ->table(array(
	        C('DB_PREFIX').'user_message'=>'M',
	        C('DB_PREFIX').'user'=>'U',
	    )
	    )
	    ->where('M.userid=U.userid and M.status=true and M.infoid='.$id.' and M.type=4 and M.parent_id=0')
	    ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
	    ->order('M.create_time DESC')
	    ->limit($page->firstRow . ',' . $page->listRows)
	    ->select();

	    foreach($message as $k=>$v)
	    {
	        $child_temp = array();
	        $child_temp = $unionModel->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
	    
	        $message[$k]['childlist'] = array();
	        if($child_temp)
	        {
	            foreach($child_temp as $yk => $yv)
	            {
	                if($yv['userid'] == 0)
	                {
        				$child_temp[$yk]['nickname'] = "烘焙圈";
        				$child_temp[$yk]['openuid'] = 0;
	                }
	                else
	                {
        				$yf_user = array();
        				$yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
        				$child_temp[$yk]['nickname'] = $yf_user['nickname'];
    				    $child_temp[$yk]['openuid'] = $yf_user['openuid'];
        				$child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
        				$child_temp[$yk]['ucity'] = $yf_user['ucity'];
        				$child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
        				$child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
	                }
	                if($yv['touserid'] != 0)
	                {
        				$child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
	                }
	                else
	                {
        				$child_temp[$yk]['touser']['nickname'] = "烘焙圈";
        				$child_temp[$yk]['touser']['openuid'] = 0;
	                }
	            }
	            $message[$k]['childlist'] = $child_temp;
	        }
	        //$message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
	        $message[$k]['tempname'] = $v['nickname'];
	        $message[$k]['touser'] = array();
	        if(!empty($v['touserid']))
	        {
	            $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
	        }
	    }
	    unset($where['parent_id']);
	    $this->msgcount = M('UserMessage')->where($where)->count();
	    $this->msgtype = 4;
	    $this->assign('msglist', $message);
	    $this->assign('page',$page->show());
	    $this->assign('type', 4);
	    
	    $share['shareUrl'] = WEB_URL.$_SERVER['REQUEST_URI'];
	    $share['shareTitle'] = $detail['title'];
	    $share['sharePic'] = WEB_URL.$detail['image'];
	    $share['shareDesc'] = hy_substr($detail['content'], 30);
	    $this->assign('share', $share);
	    $this->getJsdk();
	    
	    
	    
	    $this->display();
	}
	
	//
	public function detail2()
	{
	    $id = I('get.id');
	    if(empty($id)){
	        header('Location:'.WEB_URL.U('suipai/index'));
	        exit;
	    }
	    $model = M('Suipai');
	    $model->where(array('id' => $id))->setInc('clicknum');
	    $unionModel=new Model();
	     
	    $detail = $unionModel->table(array(
	        C('DB_PREFIX').'user'=>'U',
	        C('DB_PREFIX').'suipai'=>'S',
	    ))
	    ->where('U.userid=S.userid and S.id='.$id)
	    ->field('U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz,S.id,S.title,S.image,S.all_pic,S.content,S.clicknum,S.msgnum,S.create_time')
	    ->find();
	     
	    if(!$detail){
	        
	        header('Location:'.WEB_URL.U('suipai/index'));
	        exit;
	        
	    }
	
	    $yf_thumb = array();
	    $yf_pic = array();
	    if(!empty($detail['all_pic']))
	    {
	        $yf_arr = json_decode($detail['all_pic'], true);
	        $yf_temp = array();
	        foreach($yf_arr as $k => $v)
	        {
	            $yf_ex = explode("/", $v);
	            $yf_ex[count($yf_ex)-1] = str_replace("yf_", "", $yf_ex[count($yf_ex)-1]);
	            $yf_pic[] = $temp = WEB_URL.implode("/", $yf_ex);
	            $yf_thumb[] = array(
	                'thumb' => $v,
	                'pic' => $temp
	            );
	        }
	    }
	    else
	    {
	        $yf_arr = array();
	        $yf_thumb[] = array(
	            'thumb' => $detail['image'],
	            'pic' => WEB_URL.$detail['image']
	        );
	        $yf_pic[] = WEB_URL.$detail['image'];
	    }
	    $detail['pic_num'] = count($yf_thumb);
	    $detail['thumb'] = $yf_thumb;
	    $detail['pic'] = implode(",", $yf_pic);
	    //end
	     
	    $this->assign('data', $detail);
	     
	    $where['infoid'] = $id;
	    $where['type'] = 4;
	    $where['parent_id'] = 0;//yf 0915
	    $count = M('UserMessage')->where($where)->count();
	     
	    // $limitRows = 5; // 设置每页记录数
	    $limitRows = C('YF_PAGE');
	    $page = $this->ajaxpage($count, $limitRows, '#msg', 'ajax_div', 'ajax_page_div');
	     
	    $message=$unionModel
	    ->table(array(
	        C('DB_PREFIX').'user_message'=>'M',
	        C('DB_PREFIX').'user'=>'U',
	    )
	    )
	    ->where('M.userid=U.userid and M.status=true and M.infoid='.$id.' and M.type=4 and M.parent_id=0')
	    ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
	    ->order('M.create_time DESC')
	    ->limit($page->firstRow . ',' . $page->listRows)
	    ->select();
	
	    foreach($message as $k=>$v)
	    {
	        $child_temp = array();
	        $child_temp = $unionModel->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
	         
	        $message[$k]['childlist'] = array();
	        if($child_temp)
	        {
	            foreach($child_temp as $yk => $yv)
	            {
	                if($yv['userid'] == 0)
	                {
	                    $child_temp[$yk]['nickname'] = "烘焙圈";
	                    $child_temp[$yk]['openuid'] = 0;
	                }
	                else
	                {
	                    $yf_user = array();
	                    $yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
	                    $child_temp[$yk]['nickname'] = $yf_user['nickname'];
	                    $child_temp[$yk]['openuid'] = $yf_user['openuid'];
	                    $child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
	                    $child_temp[$yk]['ucity'] = $yf_user['ucity'];
	                    $child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
	                    $child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
	                }
	                if($yv['touserid'] != 0)
	                {
	                    $child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
	                }
	                else
	                {
	                    $child_temp[$yk]['touser']['nickname'] = "烘焙圈";
	                    $child_temp[$yk]['touser']['openuid'] = 0;
	                }
	            }
	            $message[$k]['childlist'] = $child_temp;
	        }
	        //$message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
	        $message[$k]['tempname'] = $v['nickname'];
	        $message[$k]['touser'] = array();
	        if(!empty($v['touserid']))
	        {
	            $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
	        }
	    }
	    unset($where['parent_id']);
	    $this->msgcount = M('UserMessage')->where($where)->count();
	    $this->msgtype = 4;
	    $this->assign('msglist', $message);
	    $this->assign('page',$page->show());
	    $this->assign('type', 4);
	     
	    $share['shareUrl'] = WEB_URL.$_SERVER['REQUEST_URI'];
	    $share['shareTitle'] = $detail['title'];
	    $share['sharePic'] = WEB_URL.$detail['image'];
	    $share['shareDesc'] = hy_substr($detail['content'], 30);
	    $this->assign('share', $share);
	    $this->getJsdk();
	     
	     
	     
	    $this->display();
	}
	
	
	
}
